package value

import (
	"fmt"
	"strings"
	"time"
)

type TqtDateTime string

type TqtDate string

type TqtTime string

func (t TqtDateTime) String() string {
	return string(t)
}

func (t TqtDate) String() string {
	return string(t)
}

func (t TqtTime) String() string {
	return string(t)
}

const (
	DatetimeFormat = "2006/1/2 15:04:05"
)

// ToUnixMilliByLogicalRegionId 通过逻辑大区将日期时间转为毫秒级别时间戳
func ToUnixMilliByLogicalRegionId(tqtDate string, tqtTime string, timeZone *time.Location) (svrTime int64, err error) {
	ts, err := time.ParseInLocation(DatetimeFormat, fmt.Sprintf("%s %s", tqtDate, tqtTime), timeZone)
	if err != nil {
		return
	}
	svrTime = ts.UnixMilli()
	return
}

// ToTimeStrByLogicalRegionId 通过逻辑大区将毫秒级别时间戳转为日期时间
func ToTimeStrByLogicalRegionId(svrTime int64, timeZone *time.Location) (tqtDate string, tqtTime string, err error) {
	datetimeStr := time.Unix(svrTime/1e3, 0).In(timeZone).Format(DatetimeFormat)
	datetimeData := strings.Split(datetimeStr, " ")
	if len(datetimeData) != 2 {
		return "", "", fmt.Errorf("invalid datetime format: %s", datetimeStr)
	}
	tqtDate, tqtTime = datetimeData[0], datetimeData[1]
	return
}
